<?php
session_start();
/// O My Friends: Personal Social Network Graph on Facebook
/// author : Minkyu Shim (minkyu.shim@gmail.com)
/// homepage : http://code.google.com/p/omyfriends

$lib_dir = dirname(__FILE__).'/lib/';
require_once $lib_dir.'config.php';
require_once $lib_dir.'facebook.php';
require_once $lib_dir.'util.php';
require_once $lib_dir.'friend.php';
require_once $lib_dir.'friends.php';
require_once $lib_dir.'view.php';
require_once $lib_dir.'init.php';

$me = null;

if($token){
    try{
        $me = $facebook->api('/me');
        
    }catch(Exception $e){
        error_log($e);
    }
}

/*
 * Build up output HTML
 */
//Get Template
$html = getTemplateHTML("Personal Social Network Graph");
$appletHtml = file_get_contents('./applet/applet.html');

//Replace custom tag with a real value
$html = str_ireplace("[##_LOGINOUT_BUTTON_##]", ($me)? $logout_button : $login_button, $html);
$html = str_ireplace("[##_FACEBOOK_ASYNC_LOADING_CODE_##]", null , $html);
$html = str_ireplace("[##_EXIST_SESSION_##]", ($me) ? true : null, $html);
$html = str_ireplace("[##_GRAPH_APPLET_##]", ($me) ? $appletHtml : null , $html);
$html = str_ireplace("[##_DATA_TYPE_FLAGS_##]", json_encode($data_type_flags), $html);
$html = str_ireplace("[##_APP_DIR_##]", APP_DIR, $html);
//$html = str_ireplace("[##_QUERY_STRING_##]", isset($_REQUEST['signed_request']) ? "signed_request=".$_REQUEST['signed_request'] : "signed_request=".generateSignedRequest($session), $html);
//$html = str_ireplace("[##_QUERY_STRING_##]", null, $html);

$html = str_ireplace("[##_FRIEND_NAME_##]", null, $html);

//Link URL for Friendship pages
if(stripos($me['link'], '?')){
    $link = $me['link']."&";
}else {
    $link = $me['link']."?";
}
$html = str_ireplace("[##_MY_LINK_URL_##]", $link, $html);

if(!ob_start("ob_gzhandler")) ob_start();
print $html;
ob_end_flush();
